Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Add BeforeAll and AfterAll hooks #1876

Merged
merged 34 commits into from
Apr 3, 2021

Conversation

mpkorstanje
Copy link
Contributor

@mpkorstanje mpkorstanje commented Jan 26, 2020

Summary

BeforeAll and AfterAll hooks are executed before all scenarios are executed and
after all scenarios have been executed. A hook is declared by annotating a method.
This methods must be static and do not take any arguments.

Hooks are global, all hooks declared in any step definition class will be
executed. The order in which hooks are executed is not defined. An explicit
order can be provided by using the order property in the annotation.

package io.cucumber.example;

import io.cucumber.java.AfterAll;
import io.cucumber.java.BeforeAll;

public class StepDefinitions {

    @BeforeAll
    public static void beforeAll() {
        // Runs before all scenarios
    }

    @AfterAll
    public static void afterAll() {
        // Runs after all scenarios
    }
}

Notes:

  1. When used in combination with Junit 5, Maven Surefire, and/or Failsafe use
    version 3.0.0-M5 or later.
  2. When used in combination with Junit 5 and InteliJ IDEA failures in before
    all and after all hooks do not fail a test run.

Fixes: #515.

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Work in progress to fix #515.

TODO:
 - [ ] How to deal with failure?
  - [ ] Test with JUnit4
  - [ ] Test with JUnit5
  - [ ] Test with TestNG
  - [ ] Test with CLI
  - [ ] Invoke around semantics?
 - [ ] How to report execution results?
  - [ ] TeamCity Plugin
  - [ ] Pretty formatter
  - [ ] Messages/Events
@mpkorstanje mpkorstanje added this to the 5.x.x milestone Jan 26, 2020
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 87.604% when pulling 2f4e9da on before-all-and-after-all-hooks into 218dcae on master.

@mpkorstanje
Copy link
Contributor Author

mpkorstanje commented Feb 19, 2020

To avoid conflicting with JUnit annotations it might be a good idea use names that are relatively unique. For example BeforeEachScenario and BeforeAllScenarios. This will ensure people don't import the wrong annnotation by accident.

@Naumansh
Copy link

Is there any tentative timeline on it and will it work equally good with parallel executions as well?

@mpkorstanje
Copy link
Contributor Author

mpkorstanje commented Mar 22, 2020

This is an opensource project so there are no promises, timetables or guarantees. It will be done when it is done.

It will work as expected with parallel execution within one instance of Cucumber. Because a static context is used for the before hooks different runners (e.g. from the temyers/cucumber-jvm-parallel-plugin) will interfere with each other when executed concurrently.

So effectively it will be the same as using JUnits 4 @Before/AfterClass annotated methods or JUnit 5s @Before/AfterAll annotated methods. Note that the cucumber-junit will play nicely with both JUnit 4 class rules and the @Before/AfterClass annotations.

@Naumansh
Copy link

@mpkorstanje Just confirming . So the crux is it won't work in forked mode or multi jvm?

@mpkorstanje
Copy link
Contributor Author

No. As I said, it will work as expected with parallel execution within one instance of Cucumber. For other situations you're dependent on the implementation of your hooks.

@codecov
Copy link

codecov bot commented Feb 28, 2021

Codecov Report

Merging #1876 (67889c2) into v7.x.x (018c315) will increase coverage by 0.08%.
The diff coverage is 88.64%.

Impacted file tree graph

@@             Coverage Diff              @@
##             v7.x.x    #1876      +/-   ##
============================================
+ Coverage     83.27%   83.35%   +0.08%     
- Complexity     2313     2340      +27     
============================================
  Files           298      299       +1     
  Lines          8256     8374     +118     
  Branches        750      762      +12     
============================================
+ Hits           6875     6980     +105     
- Misses         1084     1096      +12     
- Partials        297      298       +1     
Impacted Files Coverage Δ Complexity Δ
core/src/main/java/io/cucumber/core/cli/Main.java 0.00% <ø> (ø) 0.00 <0.00> (ø)
...in/java/io/cucumber/core/plugin/JsonFormatter.java 73.72% <0.00%> (ø) 51.00 <0.00> (ø)
...ava/io/cucumber/core/plugin/ProgressFormatter.java 77.14% <ø> (+3.45%) 4.00 <0.00> (ø)
.../src/main/java/io/cucumber/core/runner/Runner.java 91.58% <56.25%> (-6.22%) 33.00 <4.00> (+4.00) ⬇️
...main/java/io/cucumber/java/JavaHookDefinition.java 90.00% <66.66%> (-2.60%) 10.00 <0.00> (+1.00) ⬇️
.../src/main/java/io/cucumber/java/MethodScanner.java 82.35% <80.00%> (+1.10%) 35.00 <4.00> (+1.00)
...ucumber/core/runtime/CucumberExecutionContext.java 90.90% <83.33%> (-1.52%) 16.00 <2.00> (+4.00) ⬇️
...ava/io/cucumber/java/JavaStaticHookDefinition.java 85.00% <85.00%> (ø) 6.00 <6.00> (?)
...rc/main/java/io/cucumber/core/runtime/Runtime.java 92.45% <90.00%> (-0.69%) 13.00 <2.00> (+1.00) ⬇️
...ber/core/exception/CompositeCucumberException.java 100.00% <100.00%> (ø) 1.00 <1.00> (-3.00)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 018c315...67889c2. Read the comment docs.

@mpkorstanje mpkorstanje added this to In progress in JUnit5 via automation Mar 6, 2021
@mpkorstanje mpkorstanje mentioned this pull request Mar 8, 2021
@mpkorstanje mpkorstanje force-pushed the before-all-and-after-all-hooks branch from b91f148 to 01e9ca4 Compare March 28, 2021 01:03
@mpkorstanje mpkorstanje changed the base branch from main to v7.x.x April 2, 2021 22:17
@mpkorstanje mpkorstanje marked this pull request as ready for review April 3, 2021 19:45
@mpkorstanje mpkorstanje merged commit 3bc80b9 into v7.x.x Apr 3, 2021
JUnit5 automation moved this from In progress to Done Apr 3, 2021
@mpkorstanje mpkorstanje deleted the before-all-and-after-all-hooks branch April 3, 2021 21:28
@anaega
Copy link

anaega commented Jun 8, 2022

Hello! I am trying to use @BeforeAll but it doesn't work for me..nothing happens, it seems that this method is never used.

import io.cucumber.java.Before;
import io.cucumber.java.BeforeAll;

public class BeforeSteps {

    @BeforeAll
    public static void beforeAll() {
        log.info("Log BeforeAlL");
    }
}
<properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <serenity.version>3.2.3</serenity.version>
       <cucumber.version>7.2.3</cucumber.version>
       <lombok.version>1.18.22</lombok.version>
       <slf4j.version>1.7.36</slf4j.version>
       <logback.version>1.2.10</logback.version>
       <rest.assured.version>4.5.0</rest.assured.version>
       <postgres.version>42.2.5</postgres.version>
   </properties>
   <dependencies>
       <dependency>
           <groupId>net.serenity-bdd</groupId>
           <artifactId>serenity-core</artifactId>
           <version>${serenity.version}</version>
           <scope>test</scope>
           <exclusions>
               <exclusion>
                   <groupId>io.cucumber</groupId>
                   <artifactId>cucumber-core</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>io.cucumber</groupId>
           <artifactId>cucumber-java</artifactId>
           <version>${cucumber.version}</version>
       </dependency>
       <dependency>
           <groupId>io.cucumber</groupId>
           <artifactId>cucumber-junit</artifactId>
           <version>${cucumber.version}</version>
       </dependency>
       <dependency>
           <groupId>net.serenity-bdd</groupId>
           <artifactId>serenity-junit</artifactId>
           <version>${serenity.version}</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>net.serenity-bdd</groupId>
           <artifactId>serenity-cucumber</artifactId>
           <version>${serenity.version}</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.13.2</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.assertj</groupId>
           <artifactId>assertj-core</artifactId>
           <version>3.22.0</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.hamcrest</groupId>
           <artifactId>hamcrest-all</artifactId>
           <version>1.3</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>com.google.code.gson</groupId>
           <artifactId>gson</artifactId>
           <version>2.9.0</version>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.kafka</groupId>
           <artifactId>kafka-clients</artifactId>
           <version>2.8.0</version>
       </dependency>
       <dependency>
           <groupId>com.google.guava</groupId>
           <artifactId>guava</artifactId>
           <version>31.1-jre</version>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>commons-io</groupId>
           <artifactId>commons-io</artifactId>
           <version>2.11.0</version>
       </dependency>
       <dependency>
           <groupId>net.htmlparser.jericho</groupId>
           <artifactId>jericho-html</artifactId>
           <version>3.4</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
   <build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.0.0-M5</version>
               <configuration>
                   <skip>true</skip>
               </configuration>
           </plugin>
           <plugin>
               <artifactId>maven-failsafe-plugin</artifactId>
               <version>3.0.0-M5</version>
               <configuration>
                   <includes>
                       <include>**/*Test.java</include>
                       <include>**/Test*.java</include>
                       <include>**/*TestSuite.java</include>
                       <include>**/When*.java</include>
                   </includes>

               </configuration>
               <executions>
                   <execution>
                       <goals>
                           <goal>integration-test</goal>
                           <goal>verify</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.8.0</version>
               <configuration>
                   <source>1.8</source>
                   <target>1.8</target>
               </configuration>
           </plugin>

DO you have any ideea what I can do?
Thank you!

@mpkorstanje
Copy link
Contributor Author

To report a bug you'll want to start a new issue. Commenting on old ones just hides the discussion from view.

If you do report a bug make sure you have a minimal reproducer. So remove all the parts that are not essential to reproducing the problem. You can read more about that here:

https://stackoverflow.com/help/how-to-ask

Also if you do report a bug, make sure you have the right open source project. You are using Serenity, not Cucumber. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
JUnit5
  
Done
Development

Successfully merging this pull request may close these issues.

Add @BeforeAll and @AfterAll hooks
5 participants